package com.hhx.androidrxmvp.app;

import android.app.Activity;
import android.app.Application;

import com.hhx.androidrxmvp.BuildConfig;
import com.hhx.androidrxmvp.crash.CrashHandlerUtil;
import com.hhx.androidrxmvp.dagger.component.ApplicationComponent;
import com.hhx.androidrxmvp.dagger.component.DaggerApplicationComponent;
import com.hhx.androidrxmvp.dagger.module.ApplicationModule;
import com.hhx.androidrxmvp.net.logger.LoggerUtil;

/**
 * Created by Administrator on 2019/3/18.
 */

public class BaseApplication extends Application {

    private static BaseApplication instance;
    private AQActivityLifecycleCallbacks lifecycleCallbacks;

    @Override
    public void onCreate() {
        super.onCreate();
        if (instance == null) {
            instance = this;
        }
        LoggerUtil.init(BuildConfig.DEBUG);
        //init lifecycleCallbacks
        lifecycleCallbacks = new AQActivityLifecycleCallbacks();
        registerActivityLifecycleCallbacks(lifecycleCallbacks);
        //crash
        //CrashHandler.getInstance().init(this);
        CrashHandlerUtil crashHandlerUtil = CrashHandlerUtil.getInstance();
        crashHandlerUtil.init(this);
        crashHandlerUtil.setCrashTip("很抱歉，程序出现异常，即将退出！");
    }

    public static BaseApplication getInstance() {
        return instance;
    }

    public static ApplicationComponent getApplicationComponent() {
        return DaggerApplicationComponent
                .builder()
                .applicationModule(new ApplicationModule(instance))
                .build();
    }

    public static Activity getCurrentVisibleActivity() {
        return instance.lifecycleCallbacks.getCurrentVisibleActivity();
    }

    public static boolean isInForeground() {
        return instance != null && instance.lifecycleCallbacks != null && instance.lifecycleCallbacks.isInForeground();
    }
}
